//
//  ZDBExchangeViewController.m
//  ZDB
//
//  Created by Kk on 2020/3/14.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBExchangeViewController.h"
#import "ExchangeViewHeader.h"
#import "ExchangeListCollectionViewCell.h"
#import "CHTCollectionViewWaterfallLayout.h"
#import "ZDBExchangeDetailViewController.h"
#import "ZDBExchangeModel.h"
@interface ZDBExchangeViewController ()
<UICollectionViewDataSource, CHTCollectionViewDelegateWaterfallLayout,UINavigationControllerDelegate>
@property (nonatomic, strong)  UICollectionView *collectionView;
@property (nonatomic,strong) ExchangeViewHeader * tableviewHeadView;
@property (nonatomic,strong) NoNetworkView * workView;
@end

@implementation ZDBExchangeViewController
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableviewHeadView.frame = CGRectMake(0, 0, UIScreenWidth, 165+IPhoneTop);
    self.collectionView.frame = CGRectMake(20, self.tableviewHeadView.bottom, kScreenWidth-40, kScreenHeight-(165+IPhoneTop)-TABBAR_HEIGHT);
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
      [self refushData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.modelsArry = [NSMutableArray array];
    [self creatUI];
  
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(LoginSusess) name:LoginSusessNotificationse object:nil];
       [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(LogOutSusess) name:ExitLogonNotificationse object:nil];
    
}
-(void)LoginSusess{
    
    [self refushData];
}
-(void)LogOutSusess{
    [self refushData];
}

-(void)refushData{
    
    if (![DJLoginHelper sharedInstance].is_Login) {
        [self.tableviewHeadView refushDataWith:0];
    }else{
         [self.tableviewHeadView refushDataWith:1];
    }
    
}
-(void)creatUI{
    
    self.tableviewHeadView =[[NSBundle mainBundle] loadNibNamed:@"ExchangeViewHeader" owner:self options:nil].lastObject;
    self.tableviewHeadView.frame = CGRectMake(0, 0, UIScreenWidth, 165+IPhoneTop);
    [self.view addSubview:self.tableviewHeadView];
    WEAKSELF
    [self.tableviewHeadView.chatBtn add_BtnClickHandler:^(NSInteger tag) {
        STRONGSELFFor(weakSelf);
        
        if (![DJLoginHelper sharedInstance].is_Login) {
               [[DJLoginHelper sharedInstance]dj_showLoginVC:self];
              
               return;
           }
        
        ZDContactUsBViewController * chatVC = [ZDContactUsBViewController new];
        chatVC.title = @"联系我们";
        [strongSelf.navigationController pushViewController:chatVC animated:YES];

        
    }];
    
    [self.tableviewHeadView.loginBtn add_BtnClickHandler:^(NSInteger tag) {
       // STRONGSELFFor(weakSelf);
        
        if (![DJLoginHelper sharedInstance].is_Login) {
               [[DJLoginHelper sharedInstance]dj_showLoginVC:self];
              
               return;
           }

    }];
    
    
    
    
    CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
     layout.sectionInset = UIEdgeInsetsMake(5, 0, 0, 0);
     layout.headerHeight = 0;
     layout.footerHeight = 0;
     layout.minimumColumnSpacing = 15;
     layout.minimumInteritemSpacing = 15;
     layout.columnCount = 3;
     _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, self.tableviewHeadView.bottom, kScreenWidth-40, kScreenHeight-(165+IPhoneTop)-TABBAR_HEIGHT) collectionViewLayout:layout];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ExchangeListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ExchangeListCollectionViewCell"];
     self.collectionView.showsHorizontalScrollIndicator = NO;
     self.collectionView.showsVerticalScrollIndicator = NO;
     _collectionView.dataSource = self;
     _collectionView.delegate = self;
     _collectionView.backgroundColor = [UIColor clearColor];
     [self .view addSubview:self.collectionView];
    
     
     MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
         STRONGSELFFor(weakSelf);
         [strongSelf refushData:YES];
     }];
     [header beginRefreshing];
     _collectionView.mj_header = header;
}
-(void)refushData:(BOOL)isrefush{
   // integralGoods_GetList
    NSString  * inurl = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"integralGoods_GetList"];
       WEAKSELF
       [[SmileHttpTool sharedInstance] POST :inurl parameters:nil origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
         STRONGSELFFor(weakSelf)
           
           if (statusCode==0) {
               [strongSelf.modelsArry removeAllObjects];
               NSArray * dataArry = [NSArray arrayWithArray:responseObject];
               
               strongSelf.modelsArry = [ZDBExchangeModel arrayOfModelsFromDictionaries:dataArry error:nil];
              // NSLog(@"integralGoods_GetList----%@",strongSelf.modelsArry);
               
           }else{
                [JMNotifyView showNotify:message isSuccessful:NO];
           }
           
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self placeholderViewWithFrame:self.collectionView.frame NoNetwork:NO];
             });
           // [strongSelf stopLoadingAnimation];
           [strongSelf.collectionView reloadData];
           [strongSelf.collectionView.mj_header endRefreshing];
       } failure:^(id responseObject, NSError *error) {
           NSLog(@"error----%@",error);
            STRONGSELFFor(weakSelf)
             [strongSelf.collectionView.mj_header endRefreshing];
           [JMNotifyView showNotify:@"网络错误，请重试~" isSuccessful:NO];
           // [self stopLoadingAnimation];
       }];

}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.modelsArry.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ExchangeListCollectionViewCell *cell = (ExchangeListCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ExchangeListCollectionViewCell" forIndexPath:indexPath];
    cell.modell = self.modelsArry[indexPath.item];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    ZDBExchangeDetailViewController * detailVC = [ZDBExchangeDetailViewController new];
    detailVC.title = @"商品详情";
    detailVC.modell = self.modelsArry[indexPath.item];
    [self.navigationController pushViewController:detailVC animated:YES];

}
#pragma mark - CHTCollectionViewDelegateWaterfallLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return CGSizeMake((kScreenWidth-70)/3, (kScreenWidth-40-30)/3*1.5);
 
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
 // 判断要显示的控制器是否是自己
  BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
  [self statusBarStyleIsDark:!isShowHomePage];
 [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

- (void)placeholderViewWithFrame:(CGRect)frame NoNetwork:(BOOL)NoNetwork
{
    if (self.modelsArry.count == 0)
    {
        [_workView removeFromSuperview];
        _workView = [[NoNetworkView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) NoNetwork:NoNetwork];
        [self.collectionView addSubview:_workView];
        //_workView.centerY = self.collectionView.centerY-100;
    }else
    {
        [_workView dissmiss];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
